<script setup>
  import { addressListApi } from '@/services/address'
  import { preOrderApi } from '@/services/medicine.js'
  import { createOrderApi } from '@/services/medicine.js'
  import { patientConsultPayApi } from '@/services/payment.js'
  import { ref } from 'vue'
  const props = defineProps({
    id: String
  })
  // 收货地址信息
  const addressInfo = ref({})
  //支付组件实例
  const paymentRef = ref(null)
  // 预付订单信息
  const preOrderInfo = ref({})
  ;(async () => {
    const res = await addressListApi()
    addressInfo.value = res.data.data[0]
  })()
  // 药品预付订单
  ;(async () => {
    const res = await preOrderApi({
      prescriptionId: props.id
    })
    preOrderInfo.value = res.data.data
  })()
  // 当支付弹层关闭时
  async function onPaymentClose() {
    const { confirm } = await uni.showModal({
      title: '关闭支付',
      content: '取消支付将无法发货，是否确认关闭？',
      cancelText: '仍要关闭',
      cancelColor: '#848484',
      confirmText: '继续支付',
      confirmColor: '#16C2A3'
    })

    if (!confirm) paymentRef.value.close()
  }
  //确认支付
  async function onPaymentConfirm({ index }) {
    if (index == 0) return uni.utils.toast('暂不支持')
    //生成订单id
    const res = await createOrderApi(props.id, addressInfo.value.id)
    const res2 = await patientConsultPayApi({
      orderId: res.data.data.id,
      paymentMethod: index,
      payCallback: 'http://localhost:5173/#/subpkg_medicine/pay_result/index'
    })
    window.location.href = res2.data.data.payUrl
  }
</script>

<template>
  <scroll-page background-color="#f6f6f6">
    <view class="medicine-page">
      <view class="page-header">
        <view class="order-shippment">
          <view class="region">
            <uni-icons size="16" color="#FF7702" type="location-filled" />
            {{ addressInfo.city }} {{ addressInfo.county }}
          </view>
          <view class="detail">{{ addressInfo.addressDetail }}</view>
          <view class="receiver">
            {{ addressInfo.receiver }} {{ addressInfo.mobile }}
          </view>
        </view>
      </view>

      <view class="order-shop">
        优医药房
        <text class="alt">优医质保 假一赔十</text>
      </view>
      <!-- 药品列表 -->
      <view class="medicine-list">
        <view
          v-for="medicine in preOrderInfo.medicines"
          :key="medicine.id"
          class="medicine-list-item"
        >
          <image
            class="medicine-cover"
            :src="medicine.avatar"
            mode="aspectFill"
          />
          <view class="medicine-info">
            <text class="name">{{ medicine.name }}</text>
            <text
              :class="{ symbol: medicine.prescriptionFlag === 1 }"
              class="symbol line4"
            >
              {{ medicine.specs }}
            </text>
            <text class="price">¥{{ medicine.amount }}</text>
          </view>

          <view class="quantity">x{{ medicine.quantity }}</view>
          <view class="guide">用法用量：{{ medicine.usageDosag }}</view>
        </view>
      </view>

      <!-- 订单信息 -->
      <view class="order-info">
        <uni-list :border="false">
          <uni-list-item
            title="药品金额"
            :right-text="'¥' + (preOrderInfo.payment?.toFixed(2) || '0.00')"
          />
          <uni-list-item
            title="运费"
            :right-text="'¥' + (preOrderInfo.expressFee?.toFixed(2) || '0.00')"
          />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="
              '-¥' + (preOrderInfo.couponDeduction?.toFixed(2) || '0.00')
            "
          />
          <uni-list-item
            title="实付款"
            :right-text="
              '¥' + (preOrderInfo.actualPayment?.toFixed(2) || '0.00')
            "
          />
        </uni-list>
      </view>

      <!-- 底部 -->
      <view class="toolbar">
        <view class="total-amount">
          需付款:
          <text class="number">
            ¥{{ preOrderInfo.actualPayment?.toFixed(2) || '0.00' }}
          </text>
        </view>
        <view class="buttons">
          <button class="uni-button" @click="paymentRef.open()">
            立即支付
          </button>
        </view>
      </view>
    </view>
  </scroll-page>
  <custom-payment
    @close="onPaymentClose"
    @confirm="onPaymentConfirm"
    :amount="preOrderInfo.actualPayment"
    ref="paymentRef"
  />
</template>

<style lang="scss">
  .medicine-page {
    padding-bottom: calc(env(safe-area-inset-bottom) + 200rpx);
  }

  .page-header {
    padding: 0 30rpx 30rpx;
    // 需要一个高为300的背景图
    background-color: #eaf8f6;
    overflow: hidden;

    .order-shippment {
      line-height: 1;
      padding: 30rpx;
      background-color: #fff;
      border-radius: 20rpx;
      margin-top: 30rpx;
      position: relative;

      .region {
        display: flex;
        align-items: center;
        font-size: 26rpx;
        color: #6f6f6f;
      }

      .detail {
        margin: 24rpx 0;
        font-size: 34rpx;
        color: #121826;
      }

      .receiver {
        font-size: 26rpx;
        color: #121826;
      }
    }
  }

  .order-shop {
    padding: 20rpx 30rpx;
    margin: 10rpx 0;
    line-height: 1;
    font-size: 32rpx;
    color: #121826;
    font-weight: 500;

    .alt {
      font-size: 24rpx;
      color: #979797;
      margin-left: 10rpx;
    }
  }

  .medicine-list {
    padding: 0 30rpx;
    background-color: #fff;
  }

  .medicine-list-item {
    padding: 30rpx 0;
    border-bottom: 1rpx solid #ededed;
    position: relative;

    &:last-child {
      border-bottom: none;
    }

    .medicine-cover {
      width: 180rpx;
      height: 160rpx;
      float: left;
    }

    .medicine-info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 160rpx;
      padding-left: 20rpx;

      .name {
        font-size: 30rpx;
        font-weight: 500;
        color: #121826;
      }

      .unit {
        color: #979797;
        font-size: 28rpx;
        display: flex;
        align-items: center;

        @include text-overflow(1);

        &.symbol::before {
          content: '处方药';
          line-height: 1;
          padding: 8rpx 10rpx;
          margin-right: 10rpx;
          border-radius: 4rpx;
          font-size: 24rpx;
          color: #fff;
          background-color: #16c2a3;
        }
      }

      .price {
        color: #eb5757;
        font-size: 32rpx;
      }
    }

    .quantity {
      top: 30rpx;
      right: 0;
      position: absolute;

      font-size: 30rpx;
      color: #121826;
    }

    .guide {
      clear: both;
      padding: 20rpx;
      margin-top: 30rpx;
      font-size: 28rpx;
      color: #848484;
      border-radius: 10rpx;
      background-color: #f6f6f6;
    }
  }

  .order-info {
    padding: 0 30rpx;
    margin-top: 30rpx;
    background-color: #fff;

    .right-text {
      display: flex;
      align-items: center;
    }

    .copy {
      line-height: 1;
      padding: 6rpx 20rpx 4rpx;
      margin-right: 20rpx;
      font-size: 24rpx;
      color: #16c2a3;
      background-color: #fafafa;
      border-radius: 40rpx;
      border: 1rpx solid #16c2a3;
    }

    .number {
      font-size: 32rpx;
      color: #848484;
    }

    :deep(.uni-list-item__container) {
      padding-left: 0 !important;
      padding-right: 0 !important;
    }

    :deep(.uni-list-item__content-title) {
      font-size: 32rpx !important;
      color: #3c3e42 !important;
    }

    :deep(.uni-list-item__extra-text) {
      font-size: 32rpx !important;
      color: #848484 !important;
    }

    :deep(.uni-icon-wrapper) {
      padding: 0 !important;
      margin-right: -10rpx !important;
      font-size: 36rpx !important;
      margin-top: 2rpx;
    }
  }

  .toolbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;

    display: flex;
    align-items: center;
    height: 88rpx;
    padding: 30rpx 40rpx calc(env(safe-area-inset-bottom) + 30rpx);
    background-color: #fff;

    .buttons {
      flex: 1;
      display: flex;
    }

    .uni-button {
      flex: 1;
      padding: 0 30rpx;
    }

    .total-amount {
      display: flex;
      align-items: center;
      width: 270rpx;
      font-size: 28rpx;
      color: #3c3e42;
    }

    .number {
      font-size: 40rpx;
      color: #eb5757;
      margin-left: 10rpx;
    }
  }
</style>
